/*****************************************************************************
* Copyright (c) 2019 ABUP.Co.Ltd. All rights reserved.
* File name: abup_common.c
* Description: common func
* Author: WQH
* Version: v1.0
* Date: 20190303
*****************************************************************************/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "abup_common.h"
#include "abup_define.h"
#include "softap_nv.h"

#define ABUP_DEBUG 0
#if ABUP_DEBUG
const unsigned char RES_0100KB[] = {
	0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 
	0x00, 0x00, 0x01, 0x68, 0x00, 0x00, 0x02, 0x58, 0x08, 0x02, 0x00, 0x00, 0x00, 0xC0, 0x15, 0xEB, 
	0xB2, 0x00, 0x00, 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x53, 0x6F, 0x66, 0x74, 0x77, 0x61, 0x72, 
	0x65, 0x00, 0x41, 0x64, 0x6F, 0x62, 0x65, 0x20, 0x49, 0x6D, 0x61, 0x67, 0x65, 0x52, 0x65, 0x61, 
	0x64, 0x79, 0x71, 0xC9, 0x65, 0x3C, 0x00, 0x00, 0x03, 0x20, 0x69, 0x54, 0x58, 0x74, 0x58, 0x4D, 
	0x4C, 0x3A, 0x63, 0x6F, 0x6D, 0x2E, 0x61, 0x64, 0x6F, 0x62, 0x65, 0x2E, 0x78, 0x6D, 0x70, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x3C, 0x3F, 0x78, 0x70, 0x61, 0x63, 0x6B, 0x65, 0x74, 0x20, 0x62, 0x65, 
	0x67, 0x69, 0x6E, 0x3D, 0x22, 0xEF, 0xBB, 0xBF, 0x22, 0x20, 0x69, 0x64, 0x3D, 0x22, 0x57, 0x35, 
	0x4D, 0x30, 0x4D, 0x70, 0x43, 0x65, 0x68, 0x69, 0x48, 0x7A, 0x72, 0x65, 0x53, 0x7A, 0x4E, 0x54, 
	0x63, 0x7A, 0x6B, 0x63, 0x39, 0x64, 0x22, 0x3F, 0x3E, 0x20, 0x3C, 0x78, 0x3A, 0x78, 0x6D, 0x70, 
	0x6D, 0x65, 0x74, 0x61, 0x20, 0x78, 0x6D, 0x6C, 0x6E, 0x73, 0x3A, 0x78, 0x3D, 0x22, 0x61, 0x64, 
	0x6F, 0x62, 0x65, 0x3A, 0x6E, 0x73, 0x3A, 0x6D, 0x65, 0x74, 0x61, 0x2F, 0x22, 0x20, 0x78, 0x3A, 
	0x78, 0x6D, 0x70, 0x74, 0x6B, 0x3D, 0x22, 0x41, 0x64, 0x6F, 0x62, 0x65, 0x20, 0x58, 0x4D, 0x50, 
	0x20, 0x43, 0x6F, 0x72, 0x65, 0x20, 0x35, 0x2E, 0x30, 0x2D, 0x63, 0x30, 0x36, 0x30, 0x20, 0x36, 
	0x31, 0x2E, 0x31, 0x33, 0x34, 0x37, 0x37, 0x37, 0x2C, 0x20, 0x32, 0x30, 0x31, 0x30, 0x2F, 0x30, 
	0x32, 0x2F, 0x31, 0x32, 0x2D, 0x31, 0x37, 0x3A, 0x33, 0x32, 0x3A, 0x30, 0x30, 0x20, 0x20, 0x20, 
	0x20, 0x20, 0x20, 0x20, 0x20, 0x22, 0x3E, 0x20, 0x3C, 0x72, 0x64, 0x66, 0x3A, 0x52, 0x44, 0x46, 
	0x20, 0x78, 0x6D, 0x6C, 0x6E, 0x73, 0x3A, 0x72, 0x64, 0x66, 0x3D, 0x22, 0x68, 0x74, 0x74, 0x70, 
	0x3A, 0x2F, 0x2F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x33, 0x2E, 0x6F, 0x72, 0x67, 0x2F, 0x31, 0x39, 
	0x39, 0x39, 0x2F, 0x30, 0x32, 0x2F, 0x32, 0x32, 0x2D, 0x72, 0x64, 0x66, 0x2D, 0x73, 0x79, 0x6E, 
	0x74, 0x61, 0x78, 0x2D, 0x6E, 0x73, 0x23, 0x22, 0x3E, 0x20, 0x3C, 0x72, 0x64, 0x66, 0x3A, 0x44, 
	0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x72, 0x64, 0x66, 0x3A, 0x61, 
	0x62, 0x6F, 0x75, 0x74, 0x3D, 0x22, 0x22, 0x20, 0x78, 0x6D, 0x6C, 0x6E, 0x73, 0x3A, 0x78, 0x6D, 
	0x70, 0x3D, 0x22, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x6E, 0x73, 0x2E, 0x61, 0x64, 0x6F, 
	0x62, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x2F, 0x78, 0x61, 0x70, 0x2F, 0x31, 0x2E, 0x30, 0x2F, 0x22, 
	0x20, 0x78, 0x6D, 0x6C, 0x6E, 0x73, 0x3A, 0x78, 0x6D, 0x70, 0x4D, 0x4D, 0x3D, 0x22, 0x68, 0x74, 
	0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x6E, 0x73, 0x2E, 0x61, 0x64, 0x6F, 0x62, 0x65, 0x2E, 0x63, 0x6F, 
	0x6D, 0x2F, 0x78, 0x61, 0x70, 0x2F, 0x31, 0x2E, 0x30, 0x2F, 0x6D, 0x6D, 0x2F, 0x22, 0x20, 0x78, 
	0x6D, 0x6C, 0x6E, 0x73, 0x3A, 0x73, 0x74, 0x52, 0x65, 0x66, 0x3D, 0x22, 0x68, 0x74, 0x74, 0x70, 
	0x3A, 0x2F, 0x2F, 0x6E, 0x73, 0x2E, 0x61, 0x64, 0x6F, 0x62, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x2F, 
	0x78, 0x61, 0x70, 0x2F, 0x31, 0x2E, 0x30, 0x2F, 0x73, 0x54, 0x79, 0x70, 0x65, 0x2F, 0x52, 0x65, 
	0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x52, 0x65, 0x66, 0x23, 0x22, 0x20, 0x78, 0x6D, 0x70, 0x3A, 
	0x43, 0x72, 0x65, 0x61, 0x74, 0x6F, 0x72, 0x54, 0x6F, 0x6F, 0x6C, 0x3D, 0x22, 0x41, 0x64, 0x6F, 
	0x62, 0x65, 0x20, 0x50, 0x68, 0x6F, 0x74, 0x6F, 0x73, 0x68, 0x6F, 0x70, 0x20, 0x43, 0x53, 0x35, 
	0x20, 0x57, 0x69, 0x6E, 0x64, 0x6F, 0x77, 0x73, 0x22, 0x20, 0x78, 0x6D, 0x70, 0x4D, 0x4D, 0x3A, 
	0x49, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x63, 0x65, 0x49, 0x44, 0x3D, 0x22, 0x78, 0x6D, 0x70, 0x2E, 
	0x69, 0x69, 0x64, 0x3A, 0x34, 0x35, 0x43, 0x37, 0x36, 0x41, 0x35, 0x30, 0x37, 0x34, 0x44, 0x31, 
	0x31, 0x31, 0x45, 0x31, 0x39, 0x46, 0x38, 0x44, 0x43, 0x36, 0x34, 0x32, 0x37, 0x42, 0x42, 0x31, 
	0x30, 0x37, 0x38, 0x37, 0x22, 0x20, 0x78, 0x6D, 0x70, 0x4D, 0x4D, 0x3A, 0x44, 0x6F, 0x63, 0x75, 
	0x6D, 0x65, 0x6E, 0x74, 0x49, 0x44, 0x3D, 0x22, 0x78, 0x6D, 0x70, 0x2E, 0x64, 0x69, 0x64, 0x3A, 
	0x34, 0x35, 0x43, 0x37, 0x36, 0x41, 0x35, 0x31, 0x37, 0x34, 0x44, 0x31, 0x31, 0x31, 0x45, 0x31, 
	0x39, 0x46, 0x38, 0x44, 0x43, 0x36, 0x34, 0x32, 0x37, 0x42, 0x42, 0x31, 0x30, 0x37, 0x38, 0x37, 
	0x22, 0x3E, 0x20, 0x3C, 0x78, 0x6D, 0x70, 0x4D, 0x4D, 0x3A, 0x44, 0x65, 0x72, 0x69, 0x76, 0x65, 
	0x64, 0x46, 0x72, 0x6F, 0x6D, 0x20, 0x73, 0x74, 0x52, 0x65, 0x66, 0x3A, 0x69, 0x6E, 0x73, 0x74, 
	0x61, 0x6E, 0x63, 0x65, 0x49, 0x44, 0x3D, 0x22, 0x78, 0x6D, 0x70, 0x2E, 0x69, 0x69, 0x64, 0x3A, 
	0x34, 0x35, 0x43, 0x37, 0x36, 0x41, 0x34, 0x45, 0x37, 0x34, 0x44, 0x31, 0x31, 0x31, 0x45, 0x31, 
	0x39, 0x46, 0x38, 0x44, 0x43, 0x36, 0x34, 0x32, 0x37, 0x42, 0x42, 0x31, 0x30, 0x37, 0x38, 0x37, 
	0x22, 0x20, 0x73, 0x74, 0x52, 0x65, 0x66, 0x3A, 0x64, 0x6F, 0x63, 0x75, 0x6D, 0x65, 0x6E, 0x74, 
	0x49, 0x44, 0x3D, 0x22, 0x78, 0x6D, 0x70, 0x2E, 0x64, 0x69, 0x64, 0x3A, 0x34, 0x35, 0x43, 0x37, 
	0x36, 0x41, 0x34, 0x46, 0x37, 0x34, 0x44, 0x31, 0x31, 0x31, 0x45, 0x31, 0x39, 0x46, 0x38, 0x44, 
	0x43, 0x36, 0x34, 0x32, 0x37, 0x42, 0x42, 0x31, 0x30, 0x37, 0x38, 0x37, 0x22, 0x2F, 0x3E, 0x20, 
	0x3C, 0x2F, 0x72, 0x64, 0x66, 0x3A, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6F, 
	0x6E, 0x3E, 0x20, 0x3C, 0x2F, 0x72, 0x64, 0x66, 0x3A, 0x52, 0x44, 0x46, 0x3E, 0x20, 0x3C, 0x2F, 
	0x78, 0x3A, 0x78, 0x6D, 0x70, 0x6D, 0x65, 0x74, 0x61, 0x3E, 0x20, 0x3C, 0x3F, 0x78, 0x70, 0x61, 
	0x63, 0x6B, 0x65, 0x74, 0x20, 0x65, 0x6E, 0x64, 0x3D, 0x22, 0x72, 0x22, 0x3F, 0x3E, 0x2C, 0xFE, 
	0x08, 0x68, 0x00, 0x05, 0xE4, 0xCC, 0x49, 0x44, 0x41, 0x54, 0x78, 0xDA, 0xA4, 0xBD, 0x07, 0x98, 
	0x65, 0xD7, 0x55, 0x26, 0x7A, 0x72, 0xBA, 0x39, 0x55, 0x0E, 0x5D, 0xB1, 0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xDF, 0xAB, 
	0xB6, 0x5E, 0x12, 0x32, 0x27, 0xA7, 0x8E, 0x3E, 0x70, 0xE1, 0xBE, 0xFB, 0x2E, 0xDC, 0x37, 0x73, 
	0x7C, 0x3E, 0x31, 0x58, 0xE4, 0x13, 0x09, 0x9A, 0x61, 0xA2, 0xE7, 0xFB, 0xB6, 0x6D, 0xB7, 0xDA, 
	0xCD, 0xED, 0xF2, 0x4B, 0xCF, 0xBD, 0xF4, 0x77, 0x5F, 0xFE, 0x87, 0xAF, 0x5C, 0x7C, 0x71, 0xB3, 
	0xBD, 0x0F, 0x97, 0xCD, 0xC8, 0x29, 0xDB, 0x73, 0x74, 0xC7, 0xA4, 0x29, 0xF8, 0x43, 0xE3, 0x95, 
	0x29, 0x3A, 0x08, 0x03, 0x96, 0x61, 0x65, 0x5E, 0x70, 0x3C, 0xD7, 0xF4, 0x5C, 0x3E, 0xA0, 0x8F, 
	0x64, 0xFA, 0xB2, 0xA9, 0xF4, 0xCC, 0xF4, 0xF4, 0x5E, 0x79, 0x6F, 0xBF, 0x5A, 0x9D, 0x99, 0x98, 
	0x79, 0xF4, 0x91, 0x37, 0x17, 0x4A, 0xA5, 0xD5, 0xB5, 0x15, 0xCB, 0x75, 0x5C, 0xD7, 0xAB, 0xD5, 
	0xAA, 0x95, 0x6A, 0x65, 0x7B, 0x77, 0x67, 0xA3, 0xB6, 0x63, 0xF8, 0xAE, 0xC4, 0x8B, 0x0C, 0xC3, 
	0xB8, 0xBE, 0xEB, 0x07, 0x01, 0x4D, 0x2E, 0x0B, 0x8F, 0x90, 0x0A, 0xE9, 0x80, 0x0A, 0xC3, 0x80, 
	0x62, 0x18, 0x99, 0x13, 0x39, 0x8E, 0x75, 0x7C, 0x2F, 0x30, 0xDD, 0xC0, 0xF3, 0xB2, 0x89, 0xD4, 
};
#endif

static char *Str_OEM = NULL;
static char *Str_MODEL = NULL;
static char *Str_PRODUCTID = NULL;
static char *Str_PRODUCTSEC = NULL;
static char *Str_DEVICETYPE = NULL;
static char *Str_PLATFORM = NULL;


/*
????FOTA???????AT????,??????
*/
void abup_info_point_deinit(void)
{
	if (Str_OEM != NULL)
	{
		xy_printf("ABUP-->abup_info_point_deinit xy_free(Str_OEM) is running....");
		xy_free(Str_OEM);
		Str_OEM = NULL;
	}
	if (Str_MODEL != NULL)
	{
		xy_free(Str_MODEL);
		Str_MODEL = NULL;
	}
	if (Str_PRODUCTID != NULL)
	{
		xy_free(Str_PRODUCTID);
		Str_PRODUCTID = NULL;
	}
	if (Str_PRODUCTSEC != NULL)
	{
		xy_free(Str_PRODUCTSEC);
		Str_PRODUCTSEC = NULL;
	}
	if (Str_DEVICETYPE != NULL)
	{
		xy_free(Str_DEVICETYPE);
		Str_DEVICETYPE = NULL;
	}
	if (Str_PLATFORM != NULL)
	{
		xy_free(Str_PLATFORM);
		Str_PLATFORM = NULL;
	}
}

void abup_info_point_init(void)
{
	if (Str_OEM == NULL)
	{
		xy_printf("ABUP-->abup_info_point_init xy_zalloc(Str_OEM) is running....");
		Str_OEM = xy_zalloc(OEM_LEN);
	}
	if (Str_MODEL == NULL)
	{
		Str_MODEL = xy_zalloc(MODEL_LEN);
	}
	if (Str_PRODUCTID == NULL)
	{
		Str_PRODUCTID = xy_zalloc(PRODUCTID_LEN);
	}
	if (Str_PRODUCTSEC == NULL)
	{
		Str_PRODUCTSEC = xy_zalloc(PRODUCTSEC_LEN);
	}
	if (Str_DEVICETYPE == NULL)
	{
		Str_DEVICETYPE = xy_zalloc(DEVICETYPE_LEN);
	}
	if (Str_PLATFORM == NULL)
	{
		Str_PLATFORM = xy_zalloc(PLATFORM_LEN);
	}
}
void abup_set_service_info(void)
{
	abup_info_point_init();
	
	char *save_data = (char *)xy_zalloc(OEM_LEN+MODEL_LEN+PRODUCTID_LEN+PRODUCTSEC_LEN+DEVICETYPE_LEN+PLATFORM_LEN+10);

	abup_read_nv(save_data, 16, 16);
	if (memcmp(save_data, abup_get_device_mid(), 16) != 0)	//?????mid
	{
		xy_printf("ABUP-->abup_set_service_info First Init...");
		
		sprintf((char *)save_data, "%s %s %s %s %s %s",
			ADUPS_FOTA_SERVICE_OEM,
			ADUPS_FOTA_SERVICE_MODEL,
			ADUPS_FOTA_SERVICE_PRODUCT_ID,
			ADUPS_FOTA_SERVICE_PRODUCT_SEC,
			ADUPS_FOTA_SERVICE_DEVICE_TYPE,
			ADUPS_FOTA_SERVICE_PLATFORM);
		abup_write_nv(save_data, 200, OEM_LEN+MODEL_LEN+PRODUCTID_LEN+PRODUCTSEC_LEN+DEVICETYPE_LEN+PLATFORM_LEN+5);// 137 bytes
	}
	else
	{
		abup_read_nv(save_data, 200, OEM_LEN+MODEL_LEN+PRODUCTID_LEN+PRODUCTSEC_LEN+DEVICETYPE_LEN+PLATFORM_LEN+5);
	}
	//xy_printf("ABUP-->SERVICE INFO: %s", save_data);
	sscanf(save_data, "%s %s %s %s %s %s", Str_OEM, Str_MODEL, Str_PRODUCTID, Str_PRODUCTSEC, Str_DEVICETYPE, Str_PLATFORM);

	xy_free(save_data);
	save_data = NULL;
}

char abup_imei[16] = {0};
char *abup_get_device_mid(void)
{
	if (xy_get_IMEI(abup_imei, 16) != XY_OK)
	{
		xy_printf("ABUP-->Error, no mid, use default mid.");
		strncpy(abup_imei, (char *)ABUP_DEVICE_MID, 15);
	}

	abup_imei[15] = '\0';
	return (char *)abup_imei;
}

char *abup_get_product_id(void)
{
	return (char *)Str_PRODUCTID;
}

char *abup_get_product_sec(void)
{
    return (char *)Str_PRODUCTSEC;
}

unsigned int abup_get_life_time(void)
{
    return 2000;
}

char *abup_get_manufacturer(void)
{
    return (char *)Str_OEM;
}

char *abup_get_model_number(void)
{
    return (char *)Str_MODEL;
}

char *abup_get_platform(void)
{
    return (char *)Str_PLATFORM;
}

char *abup_get_device_type(void)
{
    return (char *)Str_DEVICETYPE;
}

char *abup_get_sdk_version(void)
{
    return (char *)ADUPS_FOTA_SDK_VER;
}

char *abup_get_app_version(void)
{
    return (char *)ADUPS_FOTA_APP_VER;
}

char *abup_get_firmware_version(void)
{
    return (char *)ABUP_FIRMWARE_VERSION;
}

char *abup_get_network_type(void)
{
#if ABUP_DEBUG
	int len = sizeof(RES_0100KB);
	int i, j;
	for (i = 0, j = 0; i < len; i++)
	{
		if (RES_0100KB[i] == 0) j++;
	}
	xy_printf("ABUP-->test %X %X, %d", RES_0100KB[len - 1], RES_0100KB[len - 2], j);
#endif
    return (char *)ADUPS_FOTA_NETWORK_TYPE;
}

unsigned int abup_get_utc_time(void)
{
    return 1539884059;
}

